﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.ApplicationBlocks.Data;
using System.Configuration;
using BC;
using BE;
using COMExcel = Microsoft.Office.Interop.Excel;
using System.IO;
using System.Threading;
namespace ModuleDuoc.Report
{
    public partial class frmTK_TongHopTonKho_AT : ModuleDuoc.Base.frmTK_Base
    {
        //string sql;
        private static int redCount;
        string sql;
        Zd_BienBanKiemKe be = new Zd_BienBanKiemKe();
        Zd_BienBanKiemKeBC bc = new Zd_BienBanKiemKeBC();
        DataTable dtView = new DataTable();
        public frmTK_TongHopTonKho_AT()
        {
            InitializeComponent();
        }

        private void frmTK_TongHopTonKho_AT_Load(object sender, EventArgs e)
        {
            this.dgvData.AutoGenerateColumns = false;
            dateThoiGian.Text = DateTime.Parse(this.dateThoiGian.Value.ToString("MM/yyyy")).AddMonths(-1).ToString();
        }

        private void insertData()
        {
            sql = "zD_BC_TongHopTonKho_AT";
            //string sql = "zD_BC_KiemKeThuoc";
            int thang = Int32.Parse(dateThoiGian.Text.Substring(0, 2));
            int nam = Int32.Parse(dateThoiGian.Text.Substring(3, 4));


            System.Data.SqlClient.SqlParameter tungay = new System.Data.SqlClient.SqlParameter();
            tungay.ParameterName = "@tungay";
            tungay.DbType = DbType.DateTime;
            tungay.Direction = ParameterDirection.Input;
            tungay.Value = "01/" + dateThoiGian.Text + " 00:00:00";

            System.Data.SqlClient.SqlParameter denngay = new System.Data.SqlClient.SqlParameter();
            denngay.ParameterName = "@denngay";
            denngay.DbType = DbType.DateTime;
            denngay.Direction = ParameterDirection.Input;
            denngay.Value = DateTime.DaysInMonth(nam, thang) + "/" + dateThoiGian.Text + " 23:59:00";
            //System.Data.SqlClient.SqlParameter mavt;

            //mavt = new System.Data.SqlClient.SqlParameter("@mavt", "");
            //System.Data.SqlClient.SqlParameter manhom;
            //manhom = new System.Data.SqlClient.SqlParameter("@manhom", "");
            //System.Data.SqlClient.SqlParameter nhacc = new System.Data.SqlClient.SqlParameter("@nhacc", "");
            //System.Data.SqlClient.SqlParameter wherein = new System.Data.SqlClient.SqlParameter("@wherein", "");
            SqlHelper.ExecuteNonQuery(ConfigurationManager.AppSettings["Main.ConnectionString"], CommandType.StoredProcedure, sql,
                tungay,
                denngay);
        }
        /// <summary>
        /// Xem kết quả
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void btnView_Click(object sender, EventArgs e)
        {
            base.btnView_Click(sender, e);
            insertData(); //đưa dữ liệu vào bảng tạm
            viewDgv();
        }

        private void viewDgv()
        {
            redCount = 0;
            sql = "SELECT Stt, Ngan, MaVach, TenThuoc, DVT, Ton, ThucTe, ISNULL(thucte,0) - ISNULL(ton,0) " +
                    "as ChenhLech, DonGia, (ISNULL(thucte,0) - ISNULL(ton,0)) * ISNULL(dongia,0) as ThanhTien " +
                    "FROM zd_TongHopTonKho_AT " +
                    "WHERE ton <> 0 OR thucte <> 0 " +
                    "ORDER BY tenThuoc";

            dtView = SqlHelper.ExecuteDataset(ConfigurationManager.AppSettings["Main.ConnectionString"], CommandType.Text, sql).Tables[0];
            this.dgvData.DataSource = dtView;

            if (dtView.Rows.Count > 0)
            {
                for (int i = 0; i < dgvData.Rows.Count; i++)
                {
                    if (dgvData.Rows[i].Cells["thucte"].Value.ToString() != dgvData.Rows[i].Cells["ton"].Value.ToString())
                    {
                        dgvData.Rows[i].DefaultCellStyle.BackColor = Color.Orange;
                        redCount++;
                    }
                }
            }
            this.lblKhongkhop.Text = "Không khớp: " + redCount.ToString();
            this.lblKhop.Text = "Khớp số liệu: " + (dtView.Rows.Count - Int32.Parse(redCount.ToString())).ToString();
        }


        /// <summary>
        /// Xuất Excel
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void btnExcel_Click(object sender, EventArgs e)
        {
           // xuatExcel();
        }

        public override void btnExportReport_Click(object sender, EventArgs e)
        {
            base.btnExportReport_Click(sender, e);
            insertData(); //đưa dữ liệu vào bảng tạm
            viewDgv();
            //printReport();
            frmShowReport frm = new frmShowReport(2);
            frmShowReport.strC = frmShowReport.BC.TONGHOPTONKHO_AT;
            frmShowReport._dt = dtView;
            frmShowReport._thoiGian = dateThoiGian.Text;
            frmShowReport._am = redCount.ToString();

            //Thong tin
            frm.ShowDialog();

        }

        private void button1_Click(object sender, EventArgs e)
        {
            insertData();
            redCount = 0;
            sql = "SELECT Stt, Ngan, MaVach, TenThuoc, DVT, ISNULL(thucte,0) - ISNULL(ton,0) as ChenhLech " +
                    "FROM zd_TongHopTonKho_AT " +
                    "WHERE (ton <> 0 OR thucte <> 0) " +
                    "AND ISNULL(thucte,0) - ISNULL(ton,0) < 0 " +
                    "ORDER BY tenThuoc";

            dtView = SqlHelper.ExecuteDataset(ConfigurationManager.AppSettings["Main.ConnectionString"], CommandType.Text, sql).Tables[0];
            //this.dgvData.DataSource = dtView;

            if (dtView.Rows.Count > 0)
            {
                for (int i = 0; i < dtView.Rows.Count; i++)
                {
                    if (Int32.Parse(dtView.Rows[i]["ChenhLech"].ToString()) < 0)
                    {
                        redCount++;
                    }
                }
            }
            
            frmShowReport frm = new frmShowReport(2);
            frmShowReport.strC = frmShowReport.BC.TONGHOPTHUOCBIAM_AT;
            frmShowReport._dt = dtView;
            frmShowReport._thoiGian = dateThoiGian.Text;
            frmShowReport._am = redCount.ToString();

            //Thong tin
            frm.ShowDialog();
        }
    }
}
